﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace CoreCmsUser.Net.Utility.Utils
{
    public static class MU
    {
        public static string HttpReqJson(string url, string postStr, int timeOut)
        {
            HttpWebRequest req = null;
            HttpWebResponse rsp = null;

            #region http web request

            byte[] data = Encoding.UTF8.GetBytes(postStr);
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Timeout = timeOut * 1000;
            req.ReadWriteTimeout = timeOut * 1000;
            req.Method = "POST";
            req.KeepAlive = false;
            req.ContentType = "application/json;charset=utf-8";
            req.ServicePoint.Expect100Continue = false;//2018-8-8

            req.ContentLength = data.Length;
            using (Stream putStream = req.GetRequestStream())
            {
                putStream.Write(data, 0, data.Length);
            }

            rsp = req.GetResponse() as HttpWebResponse;
            byte[] by = new byte[800];
            using (Stream stream = rsp.GetResponseStream())
            {
                int size = 1024;
                int read = 0;
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = new byte[size];
                    do
                    {
                        read = stream.Read(buffer, 0, size);
                        ms.Write(buffer, 0, read);
                    } while (read > 0);

                    by = ms.ToArray();
                }
            }

            string strRst = Encoding.UTF8.GetString(by);
            #endregion

            if (rsp != null) rsp.Close();
            if (req != null) req.Abort();//2018-7-31，增加

            return strRst;


        }

        public static string HttpReqJson(string url, string postStr, int timeOut,bool useProxy,string proxyIp,int proxyPort)
        {
            HttpWebRequest req = null;
            HttpWebResponse rsp = null;

            #region http web request

            byte[] data = Encoding.UTF8.GetBytes(postStr);
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Timeout = timeOut * 1000;
            req.ReadWriteTimeout = timeOut * 1000;
            req.Method = "POST";
            req.KeepAlive = false;
            req.ContentType = "application/json;charset=utf-8";
            req.ServicePoint.Expect100Continue = false;//2018-8-8

            req.ContentLength = data.Length;

            if (useProxy)
            {
                WebProxy wp = new WebProxy(proxyIp, proxyPort);
                req.Proxy = wp;
            }


            using (Stream putStream = req.GetRequestStream())
            {
                putStream.Write(data, 0, data.Length);
            }

            rsp = req.GetResponse() as HttpWebResponse;
            byte[] by = new byte[800];
            using (Stream stream = rsp.GetResponseStream())
            {
                int size = 1024;
                int read = 0;
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = new byte[size];
                    do
                    {
                        read = stream.Read(buffer, 0, size);
                        ms.Write(buffer, 0, read);
                    } while (read > 0);

                    by = ms.ToArray();
                }
            }

            string strRst = Encoding.UTF8.GetString(by);
            #endregion

            if (rsp != null) rsp.Close();
            if (req != null) req.Abort();//2018-7-31，增加

            return strRst;


        }

        public static long GetTimeStampTen()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }

        /// <summary>       
        /// 10位时间戳转为C#格式时间       
        /// </summary>       
        /// <param name=”timeStamp”></param>       
        /// <returns></returns>       
        public static DateTime TimeStampToDateTime10(string timeStamp)
        {
            Int64 begtime = Convert.ToInt64(timeStamp) * 10000000;
            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度
            DateTime dt = new DateTime(time_tricks);//转化为DateTime
            return dt;
        }

    }
}
